package org.wuliu.Controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.wuliu.dao.HebaoDao;
import org.wuliu.dao.PankuDao;
import org.wuliu.dao.YeweuDao;
import org.wuliu.pojo.Hebao;
import org.wuliu.pojo.Hezhong;
import org.wuliu.pojo.Panku;
import org.wuliu.pojo.Yeweu;
import org.wuliu.utils.ResponseResult;

import java.util.List;

@Controller
@RequestMapping("/panku")
@CrossOrigin(value = "*")
public class PanKuController {
    @Autowired
    PankuDao pankuDao;
    @Autowired
    HebaoDao hebaoDao;
    @Autowired
    YeweuDao yeweuDao;

    @RequestMapping("/list")
    @ResponseBody
    public ResponseResult list(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "5") int limit) throws  Exception{
        Page<PankuDao> pager = PageHelper.startPage(page,limit);
        List<Panku> list= pankuDao.getAll();
        return new ResponseResult(pager.getTotal(),list);
    }

    @RequestMapping("/tianjia")
    @ResponseBody
    public ResponseResult tianjia(Integer yewuId ,String address){
        Panku pk=new Panku();
        pk.setChengshi(address);
        pk.setYewuId(yewuId);
        pankuDao.add(pk);
        return ResponseResult.createSuccessResUle();
    }

    @RequestMapping("/add")
    @ResponseBody
    public ResponseResult add(String idstr,String chengshi){
        String[] ids=idstr.split(",");
        pankuDao.hebaos(ids);
        Hebao hebao=new Hebao();
        hebao.setChengshi(chengshi);
        hebao.setGuanlian(idstr);
        hebaoDao.add(hebao);
        return ResponseResult.createSuccessResUle();
    }
    @RequestMapping("/chaibao")
    @ResponseBody
    public ResponseResult chaibao(String idstr,Integer id){
        String[] ids=idstr.split(",");
        pankuDao.chaibao(ids);
        hebaoDao.delete(id);
        return ResponseResult.createSuccessResUle();
    }


}
